package com.xiangxiao.rpan.data.service;

import com.xiangxiao.rpan.data.channel.RpanDataStreams;
import com.xiangxiao.rpan.data.entity.KafkaAlarm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

import javax.annotation.Resource;

/**
 * 作用: 据controller发过来的指令去生产消息, 并通过自定义通道发送出去
 * @Projectname: rpan-data
 * @Author: xiangxiao
 * @Email: 573768011@qq.com
 * @Data:2024/6/3 0:11
 */
@Service
@EnableBinding(value = {RpanDataStreams.class})
@Slf4j
public class MessageAlarmSendService {
  @Resource
  private RpanDataStreams rpanDataStreams;

  public void sendOutAlarm(KafkaAlarm msg) {
    log.info("Sending rpandata {}", msg);

    MessageChannel messageChannel = rpanDataStreams.rpanDataOutput();
    messageChannel.send(MessageBuilder
        .withPayload(msg)
        .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
        .build());
  }
}
